#include <func.h>

struct ListNode {
    int val;
    struct ListNode *next;
};

struct ListNode *detectCycle(struct ListNode *head)
{
    if (head == NULL || head->next == NULL) {
        return NULL;
    }

    struct ListNode* slow = head;
    struct ListNode* fast = head;
    while (fast && fast->next) {
        slow = slow->next;
        fast = fast->next->next;
        if (slow == fast) {
            // 有环
            slow = head;
            while (slow != fast) {
                slow = slow->next;
                fast = fast->next;
            } // slow == fast
            return slow;
        }
    } // fast == NULL || fast->next == NULL 
      
    return NULL;
}

int main(int argc, char* argv[])
{
    return 0;
}

